/*
 * =====================================================================================
 *
 *       Filename:  soring.c
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  08/22/2011 10:55:50 PM
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Zhang Ran (), orangezhr@gmail.com
 *        Company:  Sunniwell
 *
 * =====================================================================================
 */

#include <stdio.h>
#include <assert.h>

void insert_sorting(int *a, size_t size)
{
    assert(a != NULL);

    int i, j, tmp = 0;

    for(i=1; i < size; i++)
    {
        j = i - 1;

        if(a[i] < a[j])
        {
            for(j; a[j] > a[j + 1] && j >= 0; j--)
            {
                tmp = a[j];
                a[j] = a[j + 1];
                a[j + 1] = tmp;
            }
        }
    }
}

void bubble_sorting(int *a, size_t size)
{
    assert(a != NULL);

    int i;
    int j;
    int tmp;
    int n = 0;

    for (i=0; i < size; i++)
    {
        for(j = 0; j < size - i; j++)
        {
            if (a[j - 1] > a[j])
            {
                tmp = a[j - 1];
                a[j - 1] = a[j];
                a[j] = tmp;
            }
            n++;
        }
    }

    printf("->%d<-\n", n);
}

#define MAX_SIZE 3090

int main(void)
{
    int i = 0;
    int a[MAX_SIZE] = {0};
    for(i = 0; i < MAX_SIZE; i++)
    {
       a[i] = MAX_SIZE - i;
    }

    insert_sorting(a, MAX_SIZE);
    bubble_sorting(a, MAX_SIZE);
    for(i = 0;i<MAX_SIZE;i++)
    {
       printf("%d\n", a[i]);
    }

    return 0;
}
